package cn.rocky.pandacodegen.service;

import cn.rocky.pandacodegen.common.enums.DbTypeEnum;

import java.util.List;

public interface TypeFormatter {

    /**
     * 格式化
     *
     * @param columnType 列类型
     * @return {@code String}
     */
    default String format(String columnType) {
        if (isBit(columnType)) {
            return DbTypeEnum.BIT.getType();
        }
        if (isBoolean(columnType)) {
            return DbTypeEnum.BOOLEAN.getType();
        }
        if (isTinyint(columnType)) {
            return DbTypeEnum.TINYINT.getType();
        }
        if (isSmallint(columnType)) {
            return DbTypeEnum.SMALLINT.getType();
        }
        if (isInt(columnType)) {
            return DbTypeEnum.INT.getType();
        }
        if (isLong(columnType)) {
            return DbTypeEnum.BIGINT.getType();
        }
        if (isFloat(columnType)) {
            return DbTypeEnum.FLOAT.getType();
        }
        if (isDouble(columnType)) {
            return DbTypeEnum.DOUBLE.getType();
        }
        if (isDecimal(columnType)) {
            return DbTypeEnum.DECIMAL.getType();
        }
        if (isJsonb(columnType)) {
            return DbTypeEnum.JSONB.getType();
        }
        if (isVarchar(columnType)) {
            return DbTypeEnum.VARCHAR.getType();
        }
        if (isDatetime(columnType)) {
            return DbTypeEnum.DATETIME.getType();
        }
        if (isBlob(columnType)) {
            return DbTypeEnum.BLOB.getType();
        }

        return DbTypeEnum.VARCHAR.getType();
    }

    default boolean contains(List<String> columnTypes, String type) {
        for (String columnType : columnTypes) {
            if (containsIgnoreCase(type, columnType)) {
                return true;
            }
        }
        return false;
    }

    boolean isBit(String columnType);

    boolean isBoolean(String columnType);

    boolean isTinyint(String columnType);

    boolean isSmallint(String columnType);

    boolean isInt(String columnType);

    boolean isLong(String columnType);

    boolean isFloat(String columnType);

    boolean isDouble(String columnType);

    boolean isDecimal(String columnType);

    boolean isVarchar(String columnType);

    boolean isDatetime(String columnType);

    boolean isBlob(String columnType);

    boolean isJsonb(String columnType);

    static boolean containsIgnoreCase(String str, String searchStr) {
        return str != null && searchStr != null && contains(str.toUpperCase(), searchStr.toUpperCase());
    }

    /**
     * 是否包含
     *
     * @param str       str
     * @param searchStr searchStr
     * @return boolean
     */
    static boolean contains(String str, String searchStr) {
        if (str != null && searchStr != null) {
            return str.contains(searchStr);
        }
        return false;
    }
}
